<script lang="ts">
  interface User {
    name: string;
    age: number;
    email: string;
  }

  export let name: string;
  export let age: number;
  export let email: string;
  export let avatar: string = '👤';

  let isExpanded: boolean = false;

  function toggleExpanded(): void {
    isExpanded = !isExpanded;
  }

  function handleKeyDown(event: KeyboardEvent): void {
    if (event.key === 'Enter' || event.key === ' ') {
      event.preventDefault();
      toggleExpanded();
    }
  }

  function getAgeGroup(age: number): string {
    if (age < 18) return '青少年';
    if (age < 30) return '青年';
    if (age < 50) return '中年';
    return '老年';
  }
</script>

<div class="user-card" class:expanded={isExpanded}>
  <div 
    class="card-header" 
    on:click={toggleExpanded}
    on:keydown={handleKeyDown}
    role="button"
    tabindex="0"
  >
    <div class="avatar">{avatar}</div>
    <div class="user-info">
      <h3>{name}</h3>
      <p class="age">{age}岁 ({getAgeGroup(age)})</p>
    </div>
    <div class="expand-icon">
      {isExpanded ? '▼' : '▶'}
    </div>
  </div>
  
  {#if isExpanded}
    <div class="card-content">
      <div class="info-item">
        <span class="label">📧 邮箱:</span>
        <span class="value">{email}</span>
      </div>
      <div class="info-item">
        <span class="label">🎂 年龄:</span>
        <span class="value">{age}岁</span>
      </div>
      <div class="info-item">
        <span class="label">👥 年龄段:</span>
        <span class="value">{getAgeGroup(age)}</span>
      </div>
      
      <slot name="additional-info">
        <div class="default-info">
          <p>💡 点击卡片头部可以展开/收起详细信息</p>
        </div>
      </slot>
    </div>
  {/if}
</div>

<style>
  .user-card {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 15px;
    padding: 20px;
    border: 1px solid rgba(52, 152, 219, 0.2);
    transition: all 0.3s ease;
    cursor: pointer;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  }

  .user-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
  }

  .user-card.expanded {
    background: rgba(255, 255, 255, 0.98);
  }

  .card-header {
    display: flex;
    align-items: center;
    gap: 15px;
  }

  .avatar {
    font-size: 3rem;
    width: 60px;
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(52, 152, 219, 0.1);
    border-radius: 50%;
    border: 2px solid rgba(52, 152, 219, 0.3);
  }

  .user-info {
    flex: 1;
  }

  .user-info h3 {
    margin: 0 0 5px 0;
    color: #2c3e50;
    font-size: 1.3rem;
  }

  .age {
    margin: 0;
    color: #7f8c8d;
    font-size: 0.9rem;
  }

  .expand-icon {
    color: #7f8c8d;
    font-size: 1.2rem;
    transition: transform 0.3s ease;
  }

  .expanded .expand-icon {
    transform: rotate(180deg);
  }

  .card-content {
    margin-top: 20px;
    padding-top: 20px;
    border-top: 1px solid rgba(52, 152, 219, 0.2);
  }

  .info-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 0;
    border-bottom: 1px solid rgba(52, 152, 219, 0.1);
  }

  .info-item:last-child {
    border-bottom: none;
  }

  .label {
    color: #7f8c8d;
    font-weight: 500;
  }

  .value {
    color: #2c3e50;
    font-weight: bold;
  }

  .default-info {
    margin-top: 15px;
    padding: 10px;
    background: rgba(52, 152, 219, 0.1);
    border-radius: 8px;
    text-align: center;
  }

  .default-info p {
    margin: 0;
    color: #7f8c8d;
    font-size: 0.9rem;
  }

  /* 暗色主题 */
  :global(body.dark-mode) .user-card {
    background: rgba(52, 73, 94, 0.95);
    border-color: rgba(52, 152, 219, 0.3);
  }

  :global(body.dark-mode) .user-card.expanded {
    background: rgba(52, 73, 94, 0.98);
  }

  :global(body.dark-mode) .avatar {
    background: rgba(52, 152, 219, 0.2);
    border-color: rgba(52, 152, 219, 0.4);
  }

  :global(body.dark-mode) .user-info h3 {
    color: #ecf0f1;
  }

  :global(body.dark-mode) .age {
    color: #bdc3c7;
  }

  :global(body.dark-mode) .expand-icon {
    color: #bdc3c7;
  }

  :global(body.dark-mode) .card-content {
    border-top-color: rgba(52, 152, 219, 0.3);
  }

  :global(body.dark-mode) .info-item {
    border-bottom-color: rgba(52, 152, 219, 0.2);
  }

  :global(body.dark-mode) .label {
    color: #bdc3c7;
  }

  :global(body.dark-mode) .value {
    color: #ecf0f1;
  }

  :global(body.dark-mode) .default-info {
    background: rgba(52, 152, 219, 0.2);
  }

  :global(body.dark-mode) .default-info p {
    color: #bdc3c7;
  }
</style> 